package com.leo.foodmaster.admin.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leo.foodmaster.admin.system.entity.Role;
import com.leo.foodmaster.admin.system.service.IRoleService;
import com.leo.foodmaster.commons.entity.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author Leo
 * @since 2021-06-18
 */
@Api(tags = "角色信息管理")
@RestController
@AllArgsConstructor
@RequestMapping("/system/role")
public class RoleController {
    private final IRoleService roleService;

    @GetMapping("/listByOrganId")
    @ApiOperation("根据公司Id查询角色列表")
    public Result<List<Role>> listByOrganId(@ApiParam("公司Id") @RequestParam Long organId) {
        List<Role> roleList = roleService.list(Wrappers.lambdaQuery(Role.class)
                .eq(Role::getOrganId, organId));
        return Result.success(roleList);
    }

    @GetMapping("/list")
    @ApiOperation("分页条件查询角色列表")
    public Result<IPage<Role>> getRolePageList(
            @ApiParam(value = "当前页，从1开始", required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "每页记录数", required = true) @RequestParam Integer pageSize,
            @ApiParam(value = "角色名称") @RequestParam(required = false) String name,
            @ApiParam(value = "公司Id") @RequestParam(required = false) Long organId,
            @ApiParam(value = "用户状态") @RequestParam(required = false) String status,
            @ApiParam(value = "开始时间") @RequestParam(required = false) String beginTime,
            @ApiParam(value = "结束时间") @RequestParam(required = false) String endTime ) {
        IPage<Role> pageList  = roleService.getRolePageList(new Page<>(pageNum, pageSize), name,
                organId, status, beginTime, endTime);
        return Result.success(pageList);
    }

    @PostMapping
    @ApiOperation("新增角色")
    public Result<Object> addRole(@RequestBody Role role) {
        return roleService.addRole(role);
    }

    @PutMapping
    @ApiOperation("修改角色")
    public Result<Object> updateRole(@RequestBody Role role) {
        return roleService.updateRole(role);
    }

    @PutMapping("/updateStatus")
    @ApiOperation("修改角色状态")
    public Result<Object> updateStatus(@RequestBody Role role) {
        return roleService.updateStatus(role);
    }

    @DeleteMapping("/{roleIds}")
    @ApiOperation("删除角色")
    public Result<Object> deleteRole(@ApiParam("角色Id") @PathVariable String roleIds) {
        return roleService.deleteRole(roleIds);
    }

}

